﻿namespace MsForgeReboot.Web.Areas.ControlPanel.Controllers {
    #region Usings

    using System.Web.Mvc;

    using MsForgeReboot.Framework.Entities;
    using MsForgeReboot.Framework.Interfaces;
    using MsForgeReboot.Web.Attributes;

    #endregion

    public class SpeakersController : Controller {
        private readonly ISpeakerService speakerService;

        private readonly IMediaGalleryService mediaGalleryService;

        private readonly IUserService userService;

        /// <summary>
        ///     Initializes a new instance of the <see cref="SpeakersController" /> class.
        /// </summary>
        /// <param name="speakerService">The speaker service.</param>
        public SpeakersController(ISpeakerService speakerService, IMediaGalleryService mediaGalleryService) {
            this.speakerService = speakerService;
            this.mediaGalleryService = mediaGalleryService;
        }

        /// <summary>
        ///     Indexes the specified page.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>Paged list of registred Speakers</returns>
        public ActionResult Index(int page = 1, int pageSize = 10) {
            this.ViewBag.Title = "Predavački biro";
            return this.View(this.speakerService.GetList(page, pageSize));
        }

        /// <summary>
        ///     Deletes the specified URL pointer.
        /// </summary>
        /// <param name="urlPointer">The URL pointer.</param>
        /// <returns>
        ///     <see cref="JsonResult" /> with calue <c>success</c> if delete operation was successfull
        /// </returns>
        [HttpPost]
        public ActionResult Delete(string urlPointer) {
            this.speakerService.Delete(urlPointer);
            return this.Json("success", JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        ///     Creates this instance.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public ActionResult Create() {
            var model = new Speaker { Id = 0, UserProfile = new User() };
            return this.View("Edit", model);
        }

        /// <summary>
        ///     Creates this instance.
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [IgnorePropertyValidation("UserProfile.PublicName", "UserProfile.UserName")]
        public ActionResult Create(Speaker model) {
            if (model.UserProfile != null) {
                if (this.ModelState.IsValid) {
                    this.speakerService.Save(model, this.Request.Files);

                    return this.RedirectToAction("Index");
                }
            }

            return this.View("Edit", model);
        }

        [HttpGet]
        public ActionResult Edit(string urlPointer) {
            return this.View("Edit", this.speakerService.GetByUrlPointer(urlPointer));
        }

        [HttpPost]
        [IgnorePropertyValidation("UserProfile.PublicName", "UserProfile.UserName")]
        public ActionResult Edit(Speaker model) {
            if (model.UserProfile != null) {
                if (this.ModelState.IsValid) {
                    this.speakerService.Save(model, this.Request.Files);
                    return this.RedirectToAction("Index");
                }
            }

            return this.View("Edit", model);
        }
    }
}
